﻿using Dapper;
using Estsh.Core.Base;
using Estsh.Core.Dapper;
using Estsh.Core.Wms.IRepositories;
using Estsh.Core.Model.Result;
using Estsh.Core.Repositories;
using System.Collections;
using System.Data;
using System.Text;
using System.Text.Json;
using Estsh.Core.Models;
using Newtonsoft.Json.Linq;
using Estsh.Core.Model.EnumUtil;
using Estsh.Core.Util;
using Estsh.Core.Model.Extend.Models;
using Estsh.Core.Model.Extend.IFModels;

namespace Estsh.Core.Wms.Repositories
{
    public class WMSInterfaceMasterRepository : BaseRepository<SapPt>, IWMSInterfaceMasterRepository
    {
        public WMSInterfaceMasterRepository(DapperDbContext _dapperDbContext) : base(_dapperDbContext)
        {
        }


        #region 

        /// <summary>
        /// WMS把确认的盘点结果传到SAP
        /// </summary>
        /// <returns></returns>
        public List<WmsCycrInterface> GetInventoryResultsInfo()
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                StringBuilder sql = new StringBuilder(1024);
                sql.Append("SELECT top 1000 * FROM wms_cycr (nolock) ");
                sql.Append("WHERE  SYNFLG = 'N' ");
                sql.Append("ORDER BY RECTIM  ");

                return dbConn.Query<WmsCycrInterface>(sql.ToString()).ToList();
            }
        }

        /// <summary>
        /// 添加WMS把确认的盘点结果传到SAP
        /// </summary>
        /// <param name="sapCu"></param>
        /// <returns></returns>
        public SapInterfaceResult InsertInventoryResultsInfo(List<WmsCycrInterface> wmsCycrs)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                SapInterfaceResult sapInterfaceResult = new SapInterfaceResult();

                StringBuilder ifSqlStringBuilder = new StringBuilder(1024);
                ifSqlStringBuilder.Append("select * from sys_if_config (nolock) where method_name='InsertInventoryResultsInfo'");
                SysIfConfig sysIfConfig = dbConn.QueryFirst<SysIfConfig>(ifSqlStringBuilder.ToString());
                if (sysIfConfig == null)
                {
                    sapInterfaceResult.success = false;
                    sapInterfaceResult.message = "地址获取失败，请检查接口配置表。";
                    return sapInterfaceResult;
                }
                string url = sysIfConfig.IfUrl;

                StringBuilder OQSqlStringBuilder = new StringBuilder(1024);
                OQSqlStringBuilder.Append("INSERT INTO dbo.OQ(SID,CRETIM,XMLCON,ACTFLG) ");
                OQSqlStringBuilder.Append("     VALUES(@SID,@CRETIM,@XMLCON,@ACTFLG) ");

                DynamicParameters OQdynamic = new DynamicParameters();
                OQdynamic.Add("@SID", Guid.NewGuid().ToString().Replace("-", ""));
                OQdynamic.Add("@CRETIM", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                OQdynamic.Add("@XMLCON", Newtonsoft.Json.JsonConvert.SerializeObject(wmsCycrs));
                OQdynamic.Add("@ACTFLG", 'N');
                dbConn.Execute(OQSqlStringBuilder.ToString(), OQdynamic);

                SapInterfaceRequest<WmsCycrInterface> request = new Model.Result.SapInterfaceRequest<WmsCycrInterface>();
                request.sid = Guid.NewGuid().ToString().Replace("-", "");
                request.inum = sysIfConfig.IfCode;
                request.objModel = wmsCycrs;

                Task<string> jsonPost = HttpClientHelper.HttpPostByJsonMathod(url, request);
                if (string.IsNullOrEmpty(jsonPost.Result.ToString()))
                {
                    sapInterfaceResult.success = false;
                    sapInterfaceResult.message = "盘点结果接口-返回值为空";
                    return sapInterfaceResult;
                }
                if (jsonPost.Result.Contains("message"))
                {
                    SapInterfaceResult result = Newtonsoft.Json.JsonConvert.DeserializeObject<SapInterfaceResult>(jsonPost.Result);
                    if (result.success == true)
                    {
                        List<string> sqlList = new List<string>();
                        List<DynamicParameters> parametersList = new List<DynamicParameters>();
                        for (int i = 0; i < wmsCycrs.Count; i++)
                        {
                            WmsCycrInterface wmsCycr = wmsCycrs[i];
                            StringBuilder sql = new StringBuilder(1024);
                            sql.Append("update wms_cycr set ");
                            sql.Append(" SYNFLG = 'Y',SYNTIM=@SYNTIM,ERRMSG=@ERRMSG ");
                            sql.Append("where  IBLNR=@IBLNR and ZEILI=@ZEILI and MATNR=@MATNR and WERKS=@WERKS and LGORT=@LGORT");
                            sqlList.Add(sql.ToString());

                            DynamicParameters parameters = new DynamicParameters();
                            parameters.Add("@IBLNR", wmsCycr.Iblnr);//盘点凭证号
                            parameters.Add("@ZEILI", wmsCycr.Zeili);//行号
                            parameters.Add("@MATNR", wmsCycr.Matnr);//物料编码
                            parameters.Add("@WERKS", wmsCycr.Werks);//工厂
                            parameters.Add("@LGORT", wmsCycr.Lgort);//库存地点
                            parameters.Add("@SYNTIM", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));//同步时间
                            parameters.Add("@ERRMSG", result.message);//消息
                            parametersList.Add(parameters);
                        }

                        this.ExecuteTransaction(sqlList, parametersList);
                        return result;
                    }
                    else
                    {
                        List<string> sqlList = new List<string>();
                        List<DynamicParameters> parametersList = new List<DynamicParameters>();
                        for (int i = 0; i < wmsCycrs.Count; i++)
                        {
                            WmsCycrInterface wmsCycr = wmsCycrs[i];
                            StringBuilder sql = new StringBuilder(1024);
                            sql.Append("update wms_cycr set ");
                            sql.Append(" SYNFLG = 'E',SYNTIM=@SYNTIM,ERRMSG=@ERRMSG ");
                            sql.Append("where  IBLNR=@IBLNR and ZEILI=@ZEILI and MATNR=@MATNR and WERKS=@WERKS and LGORT=@LGORT");
                            sqlList.Add(sql.ToString());

                            DynamicParameters parameters = new DynamicParameters();
                            parameters.Add("@IBLNR", wmsCycr.Iblnr);//盘点凭证号
                            parameters.Add("@ZEILI", wmsCycr.Zeili);//行号
                            parameters.Add("@MATNR", wmsCycr.Matnr);//物料编码
                            parameters.Add("@WERKS", wmsCycr.Werks);//工厂
                            parameters.Add("@LGORT", wmsCycr.Lgort);//库存地点
                            parameters.Add("@SYNTIM", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));//同步时间
                            parameters.Add("@ERRMSG", result.message);//异常消息
                            parametersList.Add(parameters);
                        }

                        this.ExecuteTransaction(sqlList, parametersList);
                        return result;
                    }
                }
                else
                {
                    List<string> sqlList = new List<string>();
                    List<DynamicParameters> parametersList = new List<DynamicParameters>();
                    for (int i = 0; i < wmsCycrs.Count; i++)
                    {
                        WmsCycrInterface wmsCycr = wmsCycrs[i];
                        StringBuilder sql = new StringBuilder(1024);
                        sql.Append("update wms_cycr set ");
                        sql.Append(" SYNFLG = 'E',SYNTIM=@SYNTIM,ERRMSG=@ERRMSG ");
                        sql.Append("where  IBLNR=@IBLNR and ZEILI=@ZEILI and MATNR=@MATNR and WERKS=@WERKS and LGORT=@LGORT");
                        sqlList.Add(sql.ToString());

                        DynamicParameters parameters = new DynamicParameters();
                        parameters.Add("@IBLNR", wmsCycr.Iblnr);//盘点凭证号
                        parameters.Add("@ZEILI", wmsCycr.Zeili);//行号
                        parameters.Add("@MATNR", wmsCycr.Matnr);//物料编码
                        parameters.Add("@WERKS", wmsCycr.Werks);//工厂
                        parameters.Add("@LGORT", wmsCycr.Lgort);//库存地点
                        parameters.Add("@SYNTIM", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));//同步时间
                        parameters.Add("@ERRMSG", jsonPost.Result);//异常消息
                        parametersList.Add(parameters);
                    }

                    this.ExecuteTransaction(sqlList, parametersList);
                    SapInterfaceResult result = new SapInterfaceResult();
                    result.success = false;
                    result.message = jsonPost.Result;
                    return result;

                }
            }
        }

        /// <summary>
        /// WMS把采购收货的质检判定结果（合格入库）信息传到SAP
        /// </summary>
        /// <returns></returns>
        public List<WmsInqcInterface> GetQualityInspectionResultsInfo()
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                StringBuilder sql = new StringBuilder(1024);
                sql.Append("SELECT evrtn,prove,synflg,count(1) FROM wms_inqc (nolock) ");
                sql.Append("WHERE  synflg = 'N' ");
                sql.Append("group by evrtn,prove,synflg ");

                return dbConn.Query<WmsInqcInterface>(sql.ToString()).ToList();
            }
        }
        /// <summary>
        /// WMS把质检通知发送给ERP，让ERP依据质检通知去做质检业务
        /// </summary>
        /// <returns></returns>
        public List<WmsToqc> getQualityTOQCNoticeResultsInfo()
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                StringBuilder sql = new StringBuilder(1024);
                sql.Append("SELECT EVRTN,ETENR,COUNT(1) FROM dbo.WMS_TOQC (nolock) ");
                sql.Append("WHERE  synflg = 'N' ");
                sql.Append("GROUP BY ETENR,EVRTN ");

                return dbConn.Query<WmsToqc>(sql.ToString()).ToList();
            }
        }
        /// <summary>
        /// 添加WMS把采购收货的质检判定结果（合格入库）信息传到SAP
        /// </summary>
        /// <param name="sapCu"></param>
        /// <returns></returns>
        public SapInterfaceResult InsertQualityInspectionResultsInfo(List<WmsInqcInterface> wmsInqcs)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                SapInterfaceResult sapInterfaceResult = new SapInterfaceResult();

                StringBuilder ifSqlStringBuilder = new StringBuilder(1024);
                ifSqlStringBuilder.Append("select * from sys_if_config (nolock) where method_name='InsertQualityInspectionResultsInfo'");
                SysIfConfig sysIfConfig = dbConn.QueryFirst<SysIfConfig>(ifSqlStringBuilder.ToString());
                if (sysIfConfig == null)
                {
                    sapInterfaceResult.success = false;
                    sapInterfaceResult.message = "地址获取失败，请检查接口配置表。";
                    return sapInterfaceResult;
                }
                string url = sysIfConfig.IfUrl;//获取接口地址

                for (int iq = 0; iq < wmsInqcs.Count; iq++)
                {
                    WmsInqcInterface wmsInqc = wmsInqcs[iq];

                    StringBuilder sql = new StringBuilder(1024);
                    sql.Append("SELECT * FROM wms_inqc (nolock) ");
                    sql.Append("WHERE  synflg = 'N' and prove='" + wmsInqc.PROVE+ "' ");//根据prove字段分组发送
                    List<WmsInqcInterface> wmsInqcInterfaces=  dbConn.Query<WmsInqcInterface>(sql.ToString()).ToList();

                    StringBuilder OQSqlStringBuilder = new StringBuilder(1024);
                    OQSqlStringBuilder.Append("INSERT INTO dbo.OQ(SID,CRETIM,XMLCON,ACTFLG) ");
                    OQSqlStringBuilder.Append("     VALUES(@SID,@CRETIM,@XMLCON,@ACTFLG) ");

                    DynamicParameters OQdynamic = new DynamicParameters();
                    OQdynamic.Add("@SID", Guid.NewGuid().ToString().Replace("-", ""));
                    OQdynamic.Add("@CRETIM", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                    OQdynamic.Add("@XMLCON", Newtonsoft.Json.JsonConvert.SerializeObject(wmsInqcInterfaces));
                    OQdynamic.Add("@ACTFLG", 'N');
                    dbConn.Execute(OQSqlStringBuilder.ToString(), OQdynamic);

                    SapInterfaceRequest<WmsInqcInterface> request = new Model.Result.SapInterfaceRequest<WmsInqcInterface>();
                    request.sid = Guid.NewGuid().ToString().Replace("-", "");
                    request.inum = sysIfConfig.IfCode;//接口编码
                    request.objModel = wmsInqcInterfaces;

                    Task<string> jsonPost = HttpClientHelper.HttpPostByJsonMathod(url, request);
                    if (string.IsNullOrEmpty(jsonPost.Result))
                    {
                        sapInterfaceResult.success = false;
                        sapInterfaceResult.message = "质检结果接口-返回值为空";
                        return sapInterfaceResult;
                    }
                    if (jsonPost.Result.Contains("message"))
                    {
                        SapInterfaceResult result = Newtonsoft.Json.JsonConvert.DeserializeObject<SapInterfaceResult>(jsonPost.Result);
                        if (result.success == true)
                        {
                            List<string> sqlList = new List<string>();
                            List<DynamicParameters> parametersList = new List<DynamicParameters>();
                            for (int i = 0; i < wmsInqcInterfaces.Count; i++)
                            {
                                WmsInqcInterface inqcInterface = wmsInqcInterfaces[i];
                                sql = new StringBuilder(1024);
                                sql.Append("update wms_inqc set ");
                                sql.Append(" SYNFLG = 'Y',SYNTIM=@SYNTIM,ERRMSG=@ERRMSG ");
                                sql.Append("where  EVRTN=@EVRTN and ETENR=@ETENR and MATNR=@MATNR");
                                sqlList.Add(sql.ToString());

                                DynamicParameters parameters = new DynamicParameters();
                                parameters.Add("@EVRTN", inqcInterface.Evrtn);//ASN号
                                parameters.Add("@ETENR", inqcInterface.Etenr);//ASN item号
                                parameters.Add("@MATNR", inqcInterface.MATNR);//物料
                                parameters.Add("@SYNTIM", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));//同步时间
                                parameters.Add("@ERRMSG", result.message);//消息
                                parametersList.Add(parameters);
                            }

                            this.ExecuteTransaction(sqlList, parametersList);
                        }
                        else
                        {
                            List<string> sqlList = new List<string>();
                            List<DynamicParameters> parametersList = new List<DynamicParameters>();
                            for (int i = 0; i < wmsInqcInterfaces.Count; i++)
                            {
                                WmsInqcInterface inqcInterface = wmsInqcInterfaces[i];
                                sql = new StringBuilder(1024);
                                sql.Append("update wms_inqc set ");
                                sql.Append(" SYNFLG = 'E',SYNTIM=@SYNTIM,ERRMSG=@ERRMSG ");
                                sql.Append("where  EVRTN=@EVRTN and ETENR=@ETENR and MATNR=@MATNR");
                                sqlList.Add(sql.ToString());

                                DynamicParameters parameters = new DynamicParameters();
                                parameters.Add("@EVRTN", inqcInterface.Evrtn);//ASN号
                                parameters.Add("@ETENR", inqcInterface.Etenr);//ASN item号
                                parameters.Add("@MATNR", inqcInterface.MATNR);//物料
                                parameters.Add("@SYNTIM", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));//同步时间
                                parameters.Add("@ERRMSG", result.message);//异常消息
                                parametersList.Add(parameters);
                            }

                            this.ExecuteTransaction(sqlList, parametersList);

                        }
                    }
                    else
                    {
                        List<string> sqlList = new List<string>();
                        List<DynamicParameters> parametersList = new List<DynamicParameters>();
                        for (int i = 0; i < wmsInqcInterfaces.Count; i++)
                        {
                            WmsInqcInterface inqcInterface = wmsInqcInterfaces[i];
                            sql = new StringBuilder(1024);
                            sql.Append("update wms_inqc set ");
                            sql.Append(" SYNFLG = 'E',SYNTIM=@SYNTIM,ERRMSG=@ERRMSG ");
                            sql.Append("where  EVRTN=@EVRTN and ETENR=@ETENR and MATNR=@MATNR");
                            sqlList.Add(sql.ToString());

                            DynamicParameters parameters = new DynamicParameters();
                            parameters.Add("@EVRTN", inqcInterface.Evrtn);//ASN号
                            parameters.Add("@ETENR", inqcInterface.Etenr);//ASN item号
                            parameters.Add("@MATNR", inqcInterface.MATNR);//物料
                            parameters.Add("@SYNTIM", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));//同步时间
                            parameters.Add("@ERRMSG", jsonPost.Result);//异常消息
                            parametersList.Add(parameters);
                        }

                        this.ExecuteTransaction(sqlList, parametersList); 
                    }
                }

                SapInterfaceResult interfaceResult = new SapInterfaceResult();
                interfaceResult.success = true;
                interfaceResult.message = "质检结果接口发送成功";
                return interfaceResult;
            }
        }
        /// <summary>
        /// 添加WMS把质检通知信息传到ERP
        /// 亿日业务
        /// </summary>
        /// <param name="sapCu"></param>
        /// <returns></returns>
        public SapInterfaceResult InsertQualityToQcNoticeResultsInfo(List<WmsToqc> wmsInqcs)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                SapInterfaceResult sapInterfaceResult = new SapInterfaceResult();

                StringBuilder ifSqlStringBuilder = new StringBuilder(1024);
                ifSqlStringBuilder.Append("select * from sys_if_config (nolock) where method_name='InsertQualityToQcNoticeResultsInfo'");
                SysIfConfig sysIfConfig = dbConn.QueryFirst<SysIfConfig>(ifSqlStringBuilder.ToString());
                if (sysIfConfig == null)
                {
                    sapInterfaceResult.success = false;
                    sapInterfaceResult.message = "地址获取失败，请检查接口配置表。";
                    return sapInterfaceResult;
                }
                string url = sysIfConfig.IfUrl;//获取接口地址

                for (int iq = 0; iq < wmsInqcs.Count; iq++)
                {
                    WmsToqc wmsInqc = wmsInqcs[iq];

                    StringBuilder sql = new StringBuilder(1024);
                    sql.Append("SELECT * FROM WMS_TOQC (nolock) ");
                    sql.Append("WHERE  synflg = 'N' and ETENR='" + wmsInqc.Etenr + "' ");//根据质检单号发送
                    List<WmsToqc> wmsToqcInterfaces = dbConn.Query<WmsToqc>(sql.ToString()).ToList();

                    StringBuilder OQSqlStringBuilder = new StringBuilder(1024);
                    OQSqlStringBuilder.Append("INSERT INTO dbo.OQ(SID,CRETIM,XMLCON,ACTFLG) ");
                    OQSqlStringBuilder.Append("     VALUES(@SID,@CRETIM,@XMLCON,@ACTFLG) ");

                    DynamicParameters OQdynamic = new DynamicParameters();
                    OQdynamic.Add("@SID", Guid.NewGuid().ToString().Replace("-", ""));
                    OQdynamic.Add("@CRETIM", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                    OQdynamic.Add("@XMLCON", Newtonsoft.Json.JsonConvert.SerializeObject(wmsToqcInterfaces));
                    OQdynamic.Add("@ACTFLG", 'N');
                    dbConn.Execute(OQSqlStringBuilder.ToString(), OQdynamic);

                    SapInterfaceRequest<WmsToqc> request = new Model.Result.SapInterfaceRequest<WmsToqc>();
                    request.sid = Guid.NewGuid().ToString().Replace("-", "");
                    request.inum = sysIfConfig.IfCode;//接口编码
                    request.objModel = wmsToqcInterfaces;

                    Task<string> jsonPost = HttpClientHelper.HttpPostByJsonMathod(url, request);
                    if (string.IsNullOrEmpty(jsonPost.Result))
                    {
                        sapInterfaceResult.success = false;
                        sapInterfaceResult.message = jsonPost.Result;

                        return sapInterfaceResult;
                    }
                    if (jsonPost.Result.Contains("message"))
                    {
                        SapInterfaceResult result = Newtonsoft.Json.JsonConvert.DeserializeObject<SapInterfaceResult>(jsonPost.Result);
                        if (result.success == true)
                        {
                            List<string> sqlList = new List<string>();
                            List<DynamicParameters> parametersList = new List<DynamicParameters>();
                            for (int i = 0; i < wmsToqcInterfaces.Count; i++)
                            {
                                WmsToqc inqcInterface = wmsToqcInterfaces[i];
                                sql = new StringBuilder(1024);
                                sql.Append("update WMS_TOQC set ");
                                sql.Append(" SYNFLG = 'Y',SYNTIM=@SYNTIM,ERRMSG=@ERRMSG ");
                                sql.Append("where  EVRTN=@EVRTN and ETENR=@ETENR and MATNR=@MATNR");
                                sqlList.Add(sql.ToString());

                                DynamicParameters parameters = new DynamicParameters();
                                parameters.Add("@EVRTN", inqcInterface.Evrtn);//关联订单号
                                parameters.Add("@ETENR", inqcInterface.Etenr);//ASN item号
                                parameters.Add("@PART_NO", inqcInterface.PartNo);//物料
                                parameters.Add("@SYNTIM", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));//同步时间
                                parameters.Add("@ERRMSG", result.message);//消息
                                parametersList.Add(parameters);
                            }

                            this.ExecuteTransaction(sqlList, parametersList);
                        }
                        else
                        {
                            List<string> sqlList = new List<string>();
                            List<DynamicParameters> parametersList = new List<DynamicParameters>();
                            for (int i = 0; i < wmsToqcInterfaces.Count; i++)
                            {
                                WmsToqc inqcInterface = wmsToqcInterfaces[i];
                                sql = new StringBuilder(1024);
                                sql.Append("update WMS_TOQC set ");
                                sql.Append(" SYNFLG = 'E',SYNTIM=@SYNTIM,ERRMSG=@ERRMSG ");
                                sql.Append("where  EVRTN=@EVRTN and ETENR=@ETENR and MATNR=@MATNR");
                                sqlList.Add(sql.ToString());

                                DynamicParameters parameters = new DynamicParameters();
                                parameters.Add("@EVRTN", inqcInterface.Evrtn);//关联订单号
                                parameters.Add("@ETENR", inqcInterface.Etenr);//ASN item号
                                parameters.Add("@PART_NO", inqcInterface.PartNo);//物料
                                parameters.Add("@SYNTIM", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));//同步时间
                                parameters.Add("@ERRMSG", result.message);//异常消息
                                parametersList.Add(parameters);
                            }

                            this.ExecuteTransaction(sqlList, parametersList);

                        }
                    }
                    else
                    {
                        List<string> sqlList = new List<string>();
                        List<DynamicParameters> parametersList = new List<DynamicParameters>();
                        for (int i = 0; i < wmsToqcInterfaces.Count; i++)
                        {
                            WmsToqc inqcInterface = wmsToqcInterfaces[i];
                            sql = new StringBuilder(1024);
                            sql.Append("update WMS_TOQC set ");
                            sql.Append(" SYNFLG = 'E',SYNTIM=@SYNTIM,ERRMSG=@ERRMSG ");
                            sql.Append("where  EVRTN=@EVRTN and ETENR=@ETENR and MATNR=@MATNR");
                            sqlList.Add(sql.ToString());

                            DynamicParameters parameters = new DynamicParameters();
                            parameters.Add("@EVRTN", inqcInterface.Evrtn);//关联订单号
                            parameters.Add("@ETENR", inqcInterface.Etenr);//ASN item号
                            parameters.Add("@PART_NO", inqcInterface.PartNo);//物料
                            parameters.Add("@SYNTIM", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));//同步时间
                            parameters.Add("@ERRMSG", jsonPost.Result);//异常消息
                            parametersList.Add(parameters);
                        }

                        this.ExecuteTransaction(sqlList, parametersList);
                    }
                }

                SapInterfaceResult interfaceResult = new SapInterfaceResult();
                interfaceResult.success = true;
                interfaceResult.message = "质检结果接口发送成功";
                return interfaceResult;
            }
        }

        /// <summary>
        /// WMS把退货过帐结果传到SAP
        /// </summary>
        /// <returns></returns>
        public List<WmsRetrInterface> GetSupplierReturnResultInfo()
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                StringBuilder sql = new StringBuilder(1024);
                sql.Append("SELECT top 1000 * FROM wms_retr (nolock) ");
                sql.Append("WHERE  SYNFLG = 'N' ");
                sql.Append("ORDER BY RECTIM  ");

                return dbConn.Query<WmsRetrInterface>(sql.ToString()).ToList();
            }
        }

        /// <summary>
        /// 添加WMS把退货过帐结果传到SAP
        /// </summary>
        /// <param name="sapCu"></param>
        /// <returns></returns>
        public SapInterfaceResult InsertSupplierReturnResultInfo(List<WmsRetrInterface> wmsRetrs)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                SapInterfaceResult sapInterfaceResult = new SapInterfaceResult();

                StringBuilder ifSqlStringBuilder = new StringBuilder(1024);
                ifSqlStringBuilder.Append("select * from sys_if_config (nolock) where method_name='InsertSupplierReturnResultInfo'");
                SysIfConfig sysIfConfig = dbConn.QueryFirst<SysIfConfig>(ifSqlStringBuilder.ToString());
                if (sysIfConfig == null)
                {
                    sapInterfaceResult.success = false;
                    sapInterfaceResult.message = "地址获取失败，请检查接口配置表。";
                    return sapInterfaceResult;
                }
                string url = sysIfConfig.IfUrl;//获取接口地址



                StringBuilder OQSqlStringBuilder = new StringBuilder(1024);
                OQSqlStringBuilder.Append("INSERT INTO dbo.OQ(SID,CRETIM,XMLCON,ACTFLG) ");
                OQSqlStringBuilder.Append("     VALUES(@SID,@CRETIM,@XMLCON,@ACTFLG) ");

                DynamicParameters OQdynamic = new DynamicParameters();
                OQdynamic.Add("@SID", Guid.NewGuid().ToString().Replace("-", ""));
                OQdynamic.Add("@CRETIM", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                OQdynamic.Add("@XMLCON", Newtonsoft.Json.JsonConvert.SerializeObject(wmsRetrs));
                OQdynamic.Add("@ACTFLG", 'N');
                dbConn.Execute(OQSqlStringBuilder.ToString(), OQdynamic);

                SapInterfaceRequest<WmsRetrInterface> request = new Model.Result.SapInterfaceRequest<WmsRetrInterface>();
                request.sid = Guid.NewGuid().ToString().Replace("-", "");
                request.inum = sysIfConfig.IfCode;//接口编码
                request.objModel = wmsRetrs;

                Task<string> jsonPost = HttpClientHelper.HttpPostByJsonMathod(url, request);
                if (string.IsNullOrEmpty(jsonPost.Result))
                {
                    sapInterfaceResult.success = false;
                    sapInterfaceResult.message = "供应商退货结果接口-返回值为空";
                    return sapInterfaceResult;
                }
                if (jsonPost.Result.Contains("message"))
                {
                    SapInterfaceResult result = Newtonsoft.Json.JsonConvert.DeserializeObject<SapInterfaceResult>(jsonPost.Result);
                    if (result.success == true)
                    {
                        List<string> sqlList = new List<string>();
                        List<DynamicParameters> parametersList = new List<DynamicParameters>();
                        for (int i = 0; i < wmsRetrs.Count; i++)
                        {
                            WmsRetrInterface wmsRetr = wmsRetrs[i];
                            StringBuilder sql = new StringBuilder(1024);
                            sql.Append("update wms_retr set ");
                            sql.Append(" SYNFLG = 'Y',SYNTIM=@SYNTIM,ERRMSG=@ERRMSG ");
                            sql.Append("where ZINSTNO=@ZINSTNO and ZEILE=@ZEILE and MATNR=@MATNR");
                            sqlList.Add(sql.ToString());

                            DynamicParameters parameters = new DynamicParameters();
                            parameters.Add("@ZINSTNO", wmsRetr.Zinstno);//编号
                            parameters.Add("@ZEILE", wmsRetr.Zeile);//行号
                            parameters.Add("@MATNR", wmsRetr.Matnr);//物料编码
                            parameters.Add("@SYNTIM", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));//同步时间
                            parameters.Add("@ERRMSG", result.message);//消息
                            parametersList.Add(parameters);
                        }

                        this.ExecuteTransaction(sqlList, parametersList);
                        return result;
                    }
                    else
                    {
                        List<string> sqlList = new List<string>();
                        List<DynamicParameters> parametersList = new List<DynamicParameters>();
                        for (int i = 0; i < wmsRetrs.Count; i++)
                        {
                            WmsRetrInterface wmsRetr = wmsRetrs[i];
                            StringBuilder sql = new StringBuilder(1024);
                            sql.Append("update wms_retr set ");
                            sql.Append(" SYNFLG = 'E',SYNTIM=@SYNTIM,ERRMSG=@ERRMSG ");
                            sql.Append("where ZINSTNO=@ZINSTNO and ZEILE=@ZEILE and MATNR=@MATNR");
                            sqlList.Add(sql.ToString());

                            DynamicParameters parameters = new DynamicParameters();
                            parameters.Add("@ZINSTNO", wmsRetr.Zinstno);//编号
                            parameters.Add("@ZEILE", wmsRetr.Zeile);//行号
                            parameters.Add("@MATNR", wmsRetr.Matnr);//物料编码
                            parameters.Add("@SYNTIM", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));//同步时间
                            parameters.Add("@ERRMSG", result.message);//异常消息

                            parametersList.Add(parameters);
                        }

                        this.ExecuteTransaction(sqlList, parametersList);
                        return result;
                    }
                }
                else
                {
                    List<string> sqlList = new List<string>();
                    List<DynamicParameters> parametersList = new List<DynamicParameters>();
                    for (int i = 0; i < wmsRetrs.Count; i++)
                    {
                        WmsRetrInterface wmsRetr = wmsRetrs[i];
                        StringBuilder sql = new StringBuilder(1024);
                        sql.Append("update wms_retr set ");
                        sql.Append(" SYNFLG = 'E',SYNTIM=@SYNTIM,ERRMSG=@ERRMSG ");
                        sql.Append("where ZINSTNO=@ZINSTNO and ZEILE=@ZEILE and MATNR=@MATNR");
                        sqlList.Add(sql.ToString());

                        DynamicParameters parameters = new DynamicParameters();
                        parameters.Add("@ZINSTNO", wmsRetr.Zinstno);//编号
                        parameters.Add("@ZEILE", wmsRetr.Zeile);//行号
                        parameters.Add("@MATNR", wmsRetr.Matnr);//物料编码
                        parameters.Add("@SYNTIM", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));//同步时间
                        parameters.Add("@ERRMSG", jsonPost.Result);//异常消息

                        parametersList.Add(parameters);
                    }

                    this.ExecuteTransaction(sqlList, parametersList);
                    SapInterfaceResult result = new SapInterfaceResult();
                    result.success = false;
                    result.message = jsonPost.Result;
                    return result;
                }
            }
        }

        /// <summary>
        /// WMS把库存物料的转储（从SAP的一个库存地点到另一个库存地点，或报废出库，委外发运、零星出入库）执行结果传到SAP
        /// </summary>
        /// <returns></returns>
        public List<WmsMovrInterface> GetInventoryDumpResultInfo()
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                StringBuilder sql = new StringBuilder(1024);
                sql.Append("SELECT top 1000 * FROM wms_movr (nolock) ");
                sql.Append("WHERE  SYNFLG = 'N' ");
                sql.Append("ORDER BY RECTIM  ");

                return dbConn.Query<WmsMovrInterface>(sql.ToString()).ToList();
            }
        }

        /// <summary>
        /// 添加WMS把库存物料的转储（从SAP的一个库存地点到另一个库存地点，或报废出库，委外发运、零星出入库）执行结果传到SAP
        /// </summary>
        /// <param name="sapCu"></param>
        /// <returns></returns>
        public SapInterfaceResult InsertInventoryDumpResultInfo(List<WmsMovrInterface> wmsMovrs)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                SapInterfaceResult sapInterfaceResult = new SapInterfaceResult();

                StringBuilder ifSqlStringBuilder = new StringBuilder(1024);
                ifSqlStringBuilder.Append("select * from sys_if_config (nolock) where method_name='InsertInventoryDumpResultInfo'");
                SysIfConfig sysIfConfig = dbConn.QueryFirst<SysIfConfig>(ifSqlStringBuilder.ToString());
                if (sysIfConfig == null)
                {
                    sapInterfaceResult.success = false;
                    sapInterfaceResult.message = "地址获取失败，请检查接口配置表。";
                    return sapInterfaceResult;
                }
                string url = sysIfConfig.IfUrl;//获取接口地址



                StringBuilder OQSqlStringBuilder = new StringBuilder(1024);
                OQSqlStringBuilder.Append("INSERT INTO dbo.OQ(SID,CRETIM,XMLCON,ACTFLG) ");
                OQSqlStringBuilder.Append("     VALUES(@SID,@CRETIM,@XMLCON,@ACTFLG) ");

                DynamicParameters OQdynamic = new DynamicParameters();
                OQdynamic.Add("@SID", Guid.NewGuid().ToString().Replace("-", ""));
                OQdynamic.Add("@CRETIM", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                OQdynamic.Add("@XMLCON", Newtonsoft.Json.JsonConvert.SerializeObject(wmsMovrs));
                OQdynamic.Add("@ACTFLG", 'N');
                dbConn.Execute(OQSqlStringBuilder.ToString(), OQdynamic);

                SapInterfaceRequest<WmsMovrInterface> request = new Model.Result.SapInterfaceRequest<WmsMovrInterface>();
                request.sid = Guid.NewGuid().ToString().Replace("-", "");
                request.inum = sysIfConfig.IfCode;//接口编码
                request.objModel = wmsMovrs;

                Task<string> jsonPost = HttpClientHelper.HttpPostByJsonMathod(url, request);
                if (string.IsNullOrEmpty(jsonPost.Result))
                {
                    sapInterfaceResult.success = false;
                    sapInterfaceResult.message = "库存转储结果接口-返回值为空";
                    return sapInterfaceResult;
                }
                if (jsonPost.Result.Contains("message"))
                {
                    SapInterfaceResult result = Newtonsoft.Json.JsonConvert.DeserializeObject<SapInterfaceResult>(jsonPost.Result);
                    if (result.success == true)
                    {
                        List<string> sqlList = new List<string>();
                        List<DynamicParameters> parametersList = new List<DynamicParameters>();
                        for (int i = 0; i < wmsMovrs.Count; i++)
                        {
                            WmsMovrInterface wmsMovr = wmsMovrs[i];
                            StringBuilder sql = new StringBuilder(1024);
                            sql.Append("update wms_movr set ");
                            sql.Append(" SYNFLG = 'Y',SYNTIM=@SYNTIM,ERRMSG=@ERRMSG ");
                            sql.Append("where  ZINSTNO=@ZINSTNO and ZEILE=@ZEILE and LGORT=@LGORT and UMLGO=@UMLGO and MATNR=@MATNR");
                            sqlList.Add(sql.ToString());

                            DynamicParameters parameters = new DynamicParameters();
                            parameters.Add("@ZINSTNO", wmsMovr.Zinstno);//编号
                            parameters.Add("@ZEILE", wmsMovr.Zeile);//行号
                            parameters.Add("@LGORT", wmsMovr.Lgort);//发出库存地点
                            parameters.Add("@UMLGO", wmsMovr.Umlgo);//接收库存地点
                            parameters.Add("@MATNR", wmsMovr.Matnr);//物料编码
                            parameters.Add("@SYNTIM", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));//同步时间
                            parameters.Add("@ERRMSG", result.message);//消息
                            parametersList.Add(parameters);
                        }

                        this.ExecuteTransaction(sqlList, parametersList);
                        return result;
                    }
                    else
                    {
                        List<string> sqlList = new List<string>();
                        List<DynamicParameters> parametersList = new List<DynamicParameters>();
                        for (int i = 0; i < wmsMovrs.Count; i++)
                        {
                            WmsMovrInterface wmsMovr = wmsMovrs[i];
                            StringBuilder sql = new StringBuilder(1024);
                            sql.Append("update wms_movr set ");
                            sql.Append(" SYNFLG = 'E',SYNTIM=@SYNTIM,ERRMSG=@ERRMSG ");
                            sql.Append("where  ZINSTNO=@ZINSTNO and ZEILE=@ZEILE and LGORT=@LGORT and UMLGO=@UMLGO and MATNR=@MATNR");
                            sqlList.Add(sql.ToString());

                            DynamicParameters parameters = new DynamicParameters();
                            parameters.Add("@ZINSTNO", wmsMovr.Zinstno);//编号
                            parameters.Add("@ZEILE", wmsMovr.Zeile);//行号
                            parameters.Add("@LGORT", wmsMovr.Lgort);//发出库存地点
                            parameters.Add("@UMLGO", wmsMovr.Umlgo);//接收库存地点
                            parameters.Add("@MATNR", wmsMovr.Matnr);//物料编码
                            parameters.Add("@SYNTIM", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));//同步时间
                            parameters.Add("@ERRMSG", result.message);//异常消息
                            parametersList.Add(parameters);
                        }

                        this.ExecuteTransaction(sqlList, parametersList);
                        return result;
                    }

                }
                else
                {
                    List<string> sqlList = new List<string>();
                    List<DynamicParameters> parametersList = new List<DynamicParameters>();
                    for (int i = 0; i < wmsMovrs.Count; i++)
                    {
                        WmsMovrInterface wmsMovr = wmsMovrs[i];
                        StringBuilder sql = new StringBuilder(1024);
                        sql.Append("update wms_movr set ");
                        sql.Append(" SYNFLG = 'E',SYNTIM=@SYNTIM,ERRMSG=@ERRMSG ");
                        sql.Append("where  ZINSTNO=@ZINSTNO and ZEILE=@ZEILE and LGORT=@LGORT and UMLGO=@UMLGO and MATNR=@MATNR");
                        sqlList.Add(sql.ToString());

                        DynamicParameters parameters = new DynamicParameters();
                        parameters.Add("@ZINSTNO", wmsMovr.Zinstno);//编号
                        parameters.Add("@ZEILE", wmsMovr.Zeile);//行号
                        parameters.Add("@LGORT", wmsMovr.Lgort);//发出库存地点
                        parameters.Add("@UMLGO", wmsMovr.Umlgo);//接收库存地点
                        parameters.Add("@MATNR", wmsMovr.Matnr);//物料编码
                        parameters.Add("@SYNTIM", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));//同步时间
                        parameters.Add("@ERRMSG", jsonPost.Result);//异常消息
                        parametersList.Add(parameters);
                    }

                    this.ExecuteTransaction(sqlList, parametersList);
                    SapInterfaceResult result = new SapInterfaceResult();
                    result.success = false;
                    result.message = jsonPost.Result;
                    return result;
                }
            }
        }

        /// <summary>
        /// WMS将每次发运的信息传递给SAP,SAP用于创建交货单。
        /// </summary>
        /// <returns></returns>
        public List<WmsJisInterface> GetSalesAndShippingResultsInfo()
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                StringBuilder sql = new StringBuilder(1024);
                sql.Append("SELECT top 1000 * FROM wms_jis (nolock) ");
                sql.Append("WHERE  SYNFLG = 'N' ");
                sql.Append("ORDER BY RECTIM  ");

                return dbConn.Query<WmsJisInterface>(sql.ToString()).ToList();
            }
        }

        /// <summary>
        /// 添加WMS将每次发运的信息传递给SAP,SAP用于创建交货单。
        /// </summary>
        /// <param name="sapCu"></param>
        /// <returns></returns>
        public SapInterfaceResult InsertSalesAndShippingResultsInfo(List<WmsJisInterface> wmsJisInterfaces)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                SapInterfaceResult sapInterfaceResult = new SapInterfaceResult();

                StringBuilder ifSqlStringBuilder = new StringBuilder(1024);
                ifSqlStringBuilder.Append("select * from sys_if_config (nolock) where method_name='InsertSalesAndShippingResultsInfo'");
                SysIfConfig sysIfConfig = dbConn.QueryFirst<SysIfConfig>(ifSqlStringBuilder.ToString());
                if (sysIfConfig == null)
                {
                    sapInterfaceResult.success = false;
                    sapInterfaceResult.message = "地址获取失败，请检查接口配置表。";
                    return sapInterfaceResult;
                }
                string url = sysIfConfig.IfUrl;//获取接口地址


                StringBuilder OQSqlStringBuilder = new StringBuilder(1024);
                OQSqlStringBuilder.Append("INSERT INTO dbo.OQ(SID,CRETIM,XMLCON,ACTFLG) ");
                OQSqlStringBuilder.Append("     VALUES(@SID,@CRETIM,@XMLCON,@ACTFLG) ");

                DynamicParameters OQdynamic = new DynamicParameters();
                OQdynamic.Add("@SID", Guid.NewGuid().ToString().Replace("-", ""));
                OQdynamic.Add("@CRETIM", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                OQdynamic.Add("@XMLCON", Newtonsoft.Json.JsonConvert.SerializeObject(wmsJisInterfaces));
                OQdynamic.Add("@ACTFLG", 'N');
                dbConn.Execute(OQSqlStringBuilder.ToString(), OQdynamic);

                SapInterfaceRequest<WmsJisInterface> request = new Model.Result.SapInterfaceRequest<WmsJisInterface>();
                request.sid = Guid.NewGuid().ToString().Replace("-", "");
                request.inum = sysIfConfig.IfCode;//接口编码
                request.objModel = wmsJisInterfaces;

                Task<string> jsonPost = HttpClientHelper.HttpPostByJsonMathod(url, request);
                if (string.IsNullOrEmpty(jsonPost.Result))
                {
                    sapInterfaceResult.success = false;
                    sapInterfaceResult.message = "销售发货结果JIS接口-返回值为空";
                    return sapInterfaceResult;
                }

                if (jsonPost.Result.Contains("message"))
                {
                    SapInterfaceResult result = Newtonsoft.Json.JsonConvert.DeserializeObject<SapInterfaceResult>(jsonPost.Result);
                    if (result.success == true)
                    {
                        List<string> sqlList = new List<string>();
                        List<DynamicParameters> parametersList = new List<DynamicParameters>();
                        for (int i = 0; i < wmsJisInterfaces.Count; i++)
                        {
                            WmsJisInterface wmsJis = wmsJisInterfaces[i];
                            StringBuilder sql = new StringBuilder(1024);
                            sql.Append("update wms_jis set ");
                            sql.Append(" SYNFLG = 'Y',SYNTIM=@SYNTIM,ERRMSG=@ERRMSG ");
                            sql.Append("where  ZZ_WMSNUM=@ZZ_WMSNUM and MATNR=@MATNR");
                            sqlList.Add(sql.ToString());

                            DynamicParameters parameters = new DynamicParameters();
                            parameters.Add("@ZZ_WMSNUM", wmsJis.ZzWmsnum);//WMS发运单据号（按车）
                            parameters.Add("@MATNR", wmsJis.Matnr);//物料号
                            parameters.Add("@SYNTIM", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));//同步时间
                            parameters.Add("@ERRMSG", result.message);//消息
                            parametersList.Add(parameters);
                        }

                        this.ExecuteTransaction(sqlList, parametersList);
                        return result;
                    }
                    else
                    {
                        List<string> sqlList = new List<string>();
                        List<DynamicParameters> parametersList = new List<DynamicParameters>();
                        for (int i = 0; i < wmsJisInterfaces.Count; i++)
                        {
                            WmsJisInterface wmsJis = wmsJisInterfaces[i];
                            StringBuilder sql = new StringBuilder(1024);
                            sql.Append("update wms_jis set ");
                            sql.Append(" SYNFLG = 'E',SYNTIM=@SYNTIM,ERRMSG=@ERRMSG ");
                            sql.Append("where  ZZ_WMSNUM=@ZZ_WMSNUM and MATNR=@MATNR");
                            sqlList.Add(sql.ToString());

                            DynamicParameters parameters = new DynamicParameters();
                            parameters.Add("@ZZ_WMSNUM", wmsJis.ZzWmsnum);//WMS发运单据号（按车）
                            parameters.Add("@MATNR", wmsJis.Matnr);//物料号
                            parameters.Add("@SYNTIM", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));//同步时间
                            parameters.Add("@ERRMSG", result.message);//异常消息
                            parametersList.Add(parameters);
                        }

                        this.ExecuteTransaction(sqlList, parametersList);
                        return result;
                    }
                }
                else
                {
                    List<string> sqlList = new List<string>();
                    List<DynamicParameters> parametersList = new List<DynamicParameters>();
                    for (int i = 0; i < wmsJisInterfaces.Count; i++)
                    {
                        WmsJisInterface wmsJis = wmsJisInterfaces[i];
                        StringBuilder sql = new StringBuilder(1024);
                        sql.Append("update wms_jis set ");
                        sql.Append(" SYNFLG = 'E',SYNTIM=@SYNTIM,ERRMSG=@ERRMSG ");
                        sql.Append("where  ZZ_WMSNUM=@ZZ_WMSNUM and MATNR=@MATNR");
                        sqlList.Add(sql.ToString());

                        DynamicParameters parameters = new DynamicParameters();
                        parameters.Add("@ZZ_WMSNUM", wmsJis.ZzWmsnum);//WMS发运单据号（按车）
                        parameters.Add("@MATNR", wmsJis.Matnr);//物料号
                        parameters.Add("@SYNTIM", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));//同步时间
                        parameters.Add("@ERRMSG", jsonPost.Result);//异常消息
                        parametersList.Add(parameters);
                    }

                    this.ExecuteTransaction(sqlList, parametersList);
                    SapInterfaceResult result = new SapInterfaceResult();
                    result.success = false;
                    result.message = jsonPost.Result;
                    return result;
                }
            }
        }

        /// <summary>
        /// WMS将每次发运的信息传递给SAP,SAP用于对交货单发货。
        /// </summary>
        /// <returns></returns>
        public List<WmsNjsiInterface> GetSalesAndDeliveryPlansInfo()
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                StringBuilder sql = new StringBuilder(1024);
                sql.Append("SELECT top 1000 * FROM wms_njsi (nolock) ");
                sql.Append("WHERE  SYNFLG = 'N' ");
                sql.Append("ORDER BY RECTIM  ");

                return dbConn.Query<WmsNjsiInterface>(sql.ToString()).ToList();
            }
        }

        /// <summary>
        /// 添加WMS将每次发运的信息传递给SAP,SAP用于对交货单发货。
        /// </summary>
        /// <param name="sapCu"></param>
        /// <returns></returns>
        public SapInterfaceResult InsertSalesAndDeliveryPlanInfo(List<WmsNjsiInterface> wmsNjsis)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                SapInterfaceResult sapInterfaceResult = new SapInterfaceResult();

                StringBuilder ifSqlStringBuilder = new StringBuilder(1024);
                ifSqlStringBuilder.Append("select * from sys_if_config (nolock) where method_name='InsertSalesAndDeliveryPlanInfo'");
                SysIfConfig sysIfConfig = dbConn.QueryFirst<SysIfConfig>(ifSqlStringBuilder.ToString());
                if (sysIfConfig == null)
                {
                    sapInterfaceResult.success = false;
                    sapInterfaceResult.message = "地址获取失败，请检查接口配置表。";
                    return sapInterfaceResult;
                }
                string url = sysIfConfig.IfUrl;//获取接口地址


                StringBuilder OQSqlStringBuilder = new StringBuilder(1024);
                OQSqlStringBuilder.Append("INSERT INTO dbo.OQ(SID,CRETIM,XMLCON,ACTFLG) ");
                OQSqlStringBuilder.Append("     VALUES(@SID,@CRETIM,@XMLCON,@ACTFLG) ");

                DynamicParameters OQdynamic = new DynamicParameters();
                OQdynamic.Add("@SID", Guid.NewGuid().ToString().Replace("-", ""));
                OQdynamic.Add("@CRETIM", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                OQdynamic.Add("@XMLCON", Newtonsoft.Json.JsonConvert.SerializeObject(wmsNjsis));
                OQdynamic.Add("@ACTFLG", 'N');
                dbConn.Execute(OQSqlStringBuilder.ToString(), OQdynamic);

                SapInterfaceRequest<WmsNjsiInterface> request = new Model.Result.SapInterfaceRequest<WmsNjsiInterface>();
                request.sid = Guid.NewGuid().ToString().Replace("-", "");
                request.inum = sysIfConfig.IfCode;//接口编码
                request.objModel = wmsNjsis;

                Task<string> jsonPost = HttpClientHelper.HttpPostByJsonMathod(url, request);
                if (string.IsNullOrEmpty(jsonPost.Result))
                {
                    sapInterfaceResult.success = false;
                    sapInterfaceResult.message = "销售发货结果非JIS接口-返回值为空";
                    return sapInterfaceResult;
                }
                if (jsonPost.Result.Contains("message"))
                {
                    SapInterfaceResult result = Newtonsoft.Json.JsonConvert.DeserializeObject<SapInterfaceResult>(jsonPost.Result);
                    if (result.success == true)
                    {
                        List<string> sqlList = new List<string>();
                        List<DynamicParameters> parametersList = new List<DynamicParameters>();
                        for (int i = 0; i < wmsNjsis.Count; i++)
                        {
                            WmsNjsiInterface wmsNjsi = wmsNjsis[i];
                            StringBuilder sql = new StringBuilder(1024);
                            sql.Append("update wms_njsi set ");
                            sql.Append(" SYNFLG = 'Y',SYNTIM=@SYNTIM,ERRMSG=@ERRMSG ");
                            sql.Append("where  VBELN=@VBELN and MATNR=@MATNR");
                            sqlList.Add(sql.ToString());

                            DynamicParameters parameters = new DynamicParameters();
                            parameters.Add("@VBELN", wmsNjsi.Vbeln);//销售交货单
                            parameters.Add("@MATNR", wmsNjsi.Matnr);//物料号
                            parameters.Add("@SYNTIM", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));//同步时间
                            parameters.Add("@ERRMSG", result.message);//消息
                            parametersList.Add(parameters);
                        }

                        this.ExecuteTransaction(sqlList, parametersList);
                        return result;
                    }
                    else
                    {
                        List<string> sqlList = new List<string>();
                        List<DynamicParameters> parametersList = new List<DynamicParameters>();
                        for (int i = 0; i < wmsNjsis.Count; i++)
                        {
                            WmsNjsiInterface wmsNjsi = wmsNjsis[i];
                            StringBuilder sql = new StringBuilder(1024);
                            sql.Append("update wms_njsi set ");
                            sql.Append(" SYNFLG = 'E',SYNTIM=@SYNTIM,ERRMSG=@ERRMSG ");
                            sql.Append("where  VBELN=@VBELN and MATNR=@MATNR");
                            sqlList.Add(sql.ToString());

                            DynamicParameters parameters = new DynamicParameters();
                            parameters.Add("@VBELN", wmsNjsi.Vbeln);//销售交货单
                            parameters.Add("@MATNR", wmsNjsi.Matnr);//物料号
                            parameters.Add("@SYNTIM", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));//同步时间
                            parameters.Add("@ERRMSG", result.message);//异常消息
                            parametersList.Add(parameters);
                        }

                        this.ExecuteTransaction(sqlList, parametersList);
                        return result;
                    }
                }
                else
                {
                    List<string> sqlList = new List<string>();
                    List<DynamicParameters> parametersList = new List<DynamicParameters>();
                    for (int i = 0; i < wmsNjsis.Count; i++)
                    {
                        WmsNjsiInterface wmsNjsi = wmsNjsis[i];
                        StringBuilder sql = new StringBuilder(1024);
                        sql.Append("update wms_njsi set ");
                        sql.Append(" SYNFLG = 'E',SYNTIM=@SYNTIM,ERRMSG=@ERRMSG ");
                        sql.Append("where  VBELN=@VBELN and MATNR=@MATNR");
                        sqlList.Add(sql.ToString());

                        DynamicParameters parameters = new DynamicParameters();
                        parameters.Add("@VBELN", wmsNjsi.Vbeln);//销售交货单
                        parameters.Add("@MATNR", wmsNjsi.Matnr);//物料号
                        parameters.Add("@SYNTIM", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));//同步时间
                        parameters.Add("@ERRMSG", jsonPost.Result);//异常消息
                        parametersList.Add(parameters);
                    }

                    this.ExecuteTransaction(sqlList, parametersList);
                    SapInterfaceResult result = new SapInterfaceResult();
                    result.success = false;
                    result.message = jsonPost.Result;
                    return result;
                }

            }
        }

        /// <summary>
        /// WMS将每次发运的信息传递给SAP,SAP用于对交货单发货。
        /// </summary>
        /// <returns></returns>
        public List<MesMovtrnInterface> GetProductionReportInfo()
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                StringBuilder sql = new StringBuilder(1024);
                sql.Append("SELECT top 1000 * FROM MES_MOVTRN (nolock) ");
                sql.Append("WHERE  SYNFLG = 'N' ");
                sql.Append("ORDER BY RECTIM  ");

                return dbConn.Query<MesMovtrnInterface>(sql.ToString()).ToList();
            }
        }

        /// <summary>
        /// 添加MES发送生产汇报信息给SAP
        /// </summary>
        /// <param name="sapCu"></param>
        /// <returns></returns>
        public SapInterfaceResult InsertProductionReportInfo(List<MesMovtrnInterface> mesMovtrns)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {

                SapInterfaceResult sapInterfaceResult = new SapInterfaceResult();

                StringBuilder ifSqlStringBuilder = new StringBuilder(1024);
                ifSqlStringBuilder.Append("select * from sys_if_config (nolock) where method_name='InsertProductionReportInfo'");
                SysIfConfig sysIfConfig = dbConn.QueryFirst<SysIfConfig>(ifSqlStringBuilder.ToString());
                if (sysIfConfig == null)
                {
                    sapInterfaceResult.success = false;
                    sapInterfaceResult.message = "地址获取失败，请检查接口配置表。";
                    return sapInterfaceResult;
                }
                string url = sysIfConfig.IfUrl;//获取接口地址

                StringBuilder OQSqlStringBuilder = new StringBuilder(1024);
                OQSqlStringBuilder.Append("INSERT INTO dbo.OQ(SID,CRETIM,XMLCON,ACTFLG) ");
                OQSqlStringBuilder.Append("     VALUES(@SID,@CRETIM,@XMLCON,@ACTFLG) ");

                DynamicParameters OQdynamic = new DynamicParameters();
                OQdynamic.Add("@SID", Guid.NewGuid().ToString().Replace("-", ""));
                OQdynamic.Add("@CRETIM", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                OQdynamic.Add("@XMLCON", Newtonsoft.Json.JsonConvert.SerializeObject(mesMovtrns));
                OQdynamic.Add("@ACTFLG", 'N');
                dbConn.Execute(OQSqlStringBuilder.ToString(), OQdynamic);

                SapInterfaceRequest<MesMovtrnInterface> request = new Model.Result.SapInterfaceRequest<MesMovtrnInterface>();
                request.sid = Guid.NewGuid().ToString().Replace("-", "");
                request.inum = sysIfConfig.IfCode;//接口编码
                request.objModel = mesMovtrns;

                Task<string> jsonPost = HttpClientHelper.HttpPostByJsonMathod(url, request);
                if (string.IsNullOrEmpty(jsonPost.Result))
                {
                    sapInterfaceResult.success = false;
                    sapInterfaceResult.message = "生产汇报接口-返回值为空";
                    return sapInterfaceResult;
                }
                if (jsonPost.Result.Contains("message"))
                {
                    SapInterfaceResult result = Newtonsoft.Json.JsonConvert.DeserializeObject<SapInterfaceResult>(jsonPost.Result);
                    if (result.success == true)
                    {
                        List<string> sqlList = new List<string>();
                        List<DynamicParameters> parametersList = new List<DynamicParameters>();
                        for (int i = 0; i < mesMovtrns.Count; i++)
                        {
                            MesMovtrnInterface mesMovtrn = mesMovtrns[i];
                            StringBuilder sql = new StringBuilder(1024);
                            sql.Append("update MES_MOVTRN set ");
                            sql.Append(" SYNFLG = 'Y',SYNTIM=@SYNTIM,ERRMSG=@ERRMSG ");
                            sql.Append("where GUID=@GUID");
                            sqlList.Add(sql.ToString());

                            DynamicParameters parameters = new DynamicParameters();
                            parameters.Add("@GUID", mesMovtrn.Guid);//
                            parameters.Add("@SYNTIM", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));//同步时间
                            parameters.Add("@ERRMSG", result.message);//消息
                            parametersList.Add(parameters);
                        }

                        this.ExecuteTransaction(sqlList, parametersList);
                        return result;
                    }
                    else
                    {
                        List<string> sqlList = new List<string>();
                        List<DynamicParameters> parametersList = new List<DynamicParameters>();
                        for (int i = 0; i < mesMovtrns.Count; i++)
                        {
                            MesMovtrnInterface mesMovtrn = mesMovtrns[i];
                            StringBuilder sql = new StringBuilder(1024);
                            sql.Append("update MES_MOVTRN set ");
                            sql.Append(" SYNFLG = 'E',SYNTIM=@SYNTIM,ERRMSG=@ERRMSG ");
                            sql.Append("where GUID=@GUID");
                            sqlList.Add(sql.ToString());

                            DynamicParameters parameters = new DynamicParameters();
                            parameters.Add("@GUID", mesMovtrn.Guid);//
                            parameters.Add("@SYNTIM", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));//同步时间
                            parameters.Add("@ERRMSG", result.message);//异常消息
                            parametersList.Add(parameters);
                        }

                        this.ExecuteTransaction(sqlList, parametersList);
                        return result;
                    }
                }
                else
                {
                    List<string> sqlList = new List<string>();
                    List<DynamicParameters> parametersList = new List<DynamicParameters>();
                    for (int i = 0; i < mesMovtrns.Count; i++)
                    {
                        MesMovtrnInterface mesMovtrn = mesMovtrns[i];
                        StringBuilder sql = new StringBuilder(1024);
                        sql.Append("update MES_MOVTRN set ");
                        sql.Append(" SYNFLG = 'E',SYNTIM=@SYNTIM,ERRMSG=@ERRMSG ");
                        sql.Append("where GUID=@GUID");
                        sqlList.Add(sql.ToString());

                        DynamicParameters parameters = new DynamicParameters();
                        parameters.Add("@GUID", mesMovtrn.Guid);//
                        parameters.Add("@SYNTIM", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));//同步时间
                        parameters.Add("@ERRMSG", jsonPost.Result);//异常消息
                        parametersList.Add(parameters);
                    }

                    this.ExecuteTransaction(sqlList, parametersList);
                    SapInterfaceResult result = new SapInterfaceResult();
                    result.success = false;
                    result.message = jsonPost.Result;
                    return result;
                }
            }
        }

        #endregion
    }
}
